Micron Document
🎖️GitЯра🎖️


Displaying Raw • View renderedDownload

core/testing/README.md feat/batch-nodeinfos (a74d5d47) Text, 4.53 KB

/*
Tff7b72* Copyright (c) 2026 Meshtastic LLC
Tff7b72*
* This program is free software: you can redistribute it and/or modify
Tff7b72* it under the terms of the GNU General Public License as published by
Tff7b72* the Free Software Foundation, either version 3 of the License, or
Tff7b72* (at your option) any later version.
Tff7b72*
* This program is distributed in the hope that it will be useful,
Tff7b72* but WITHOUT ANY WARRANTY; without even the implied warranty of
Tff7b72* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Tff7b72* GNU General Public License for more details.
Tff7b72*
* You should have received a copy of the GNU General Public License
Tff7b72* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

Tc9d1d9# `:core:testing`

Tc9d1d9## Module dependency graph

<!--region graph-->
Ta5d6ff```Ta5d6ffmermaid
Ta5d6ffgraph TB
:core:testing[testing]:::kmp-library

classDef android-application fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef android-application-compose fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef compose-desktop-application fill:#CAFFBF,stroke:#000,stroke-width:2px,color:#000;
classDef android-feature fill:#FFD6A5,stroke:#000,stroke-width:2px,color:#000;
classDef android-library fill:#9BF6FF,stroke:#000,stroke-width:2px,color:#000;
classDef android-library-compose fill:#9BF6FF,stroke:#000,stroke-width:2px,color:#000;
classDef android-test fill:#A0C4FF,stroke:#000,stroke-width:2px,color:#000;
classDef jvm-library fill:#BDB2FF,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-feature fill:#FFD6A5,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-library-compose fill:#FFC1CC,stroke:#000,stroke-width:2px,color:#000;
classDef kmp-library fill:#FFC1CC,stroke:#000,stroke-width:2px,color:#000;
classDef unknown fill:#FFADAD,stroke:#000,stroke-width:2px,color:#000;

Ta5d6ff```
<!--endregion-->

Tc9d1d9## Overview
The Ta5d6ff`:core:testing` module is a dedicated **Kotlin Multiplatform (KMP)** library that provides shared test fakes, doubles, rules, and utilities. It is designed to be consumed by the Ta5d6ff`commonTest` source sets of all other KMP modules to ensure consistent and unified testing behavior across the codebase.

By centralizing fakes and mocking utilities here, we prevent duplication of test setups and enforce a standard approach to testing ViewModels, Repositories, and pure domain logic.

Tc9d1d9## Handling Platform-Specific Setup (Robolectric)

Some KMP modules interact with Android framework components (e.g., Ta5d6ff`android.net.Uri`, Ta5d6ff`androidx.room`, Ta5d6ff`DataStore`) that require Robolectric to run on the JVM. To maintain a unified test suite while providing platform-specific initialization, follow the **Subclassing Pattern**:

Tc9d1d9### 1. Create an Abstract Base Test in `commonTest`
Place your test logic in an abstract class in Ta5d6ff`src/commonTest`. Do NOT use Ta5d6ff`@BeforeTest` for setup that requires platform-specific context.

Ta5d6ff```Ta5d6ffkotlin
Tff7b72abstract Tff7b72class T56d364CommonMyViewModelTest Tb4b4b4{
Tff7b72protected Tff7b72lateinit Tff7b72var Te6edf3viewModelTb4b4b4: Te6edf3MyViewModel
T8b949e// Call this from subclasses
Tff7b72fun Td2a8ffsetupRepoTb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// ... common setup logic
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8fftestLogicTb4b4b4(Tb4b4b4) Tb4b4b4{ T8b949e/* ... */ Tb4b4b4}
Tb4b4b4}
Ta5d6ff```

Tc9d1d9### 2. Implement the JVM Subclass in `jvmTest`
A simple subclass is usually enough for pure JVM targets.

Ta5d6ff```Ta5d6ffkotlin
Tff7b72class T56d364MyViewModelTest Tb4b4b4: Te6edf3CommonMyViewModelTestTb4b4b4(Tb4b4b4) Tb4b4b4{
Tf0883e@BeforeTest
Tff7b72fun Td2a8ffsetupTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3setupRepoTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Ta5d6ff```

Tc9d1d9### 3. Implement the Android Subclass in `androidHostTest`
Use Ta5d6ff`@RunWith(RobolectricTestRunner::class)` and call Ta5d6ff`setupTestContext()` to initialize Ta5d6ff`ContextServices.app`.

Ta5d6ff```Ta5d6ffkotlin
Tf0883e@RunWithTb4b4b4(Te6edf3RobolectricTestRunnerTff7b72::Te6edf3classTb4b4b4)
Tf0883e@ConfigTb4b4b4(Te6edf3sdk Tff7b72= Tff7b72[T79c0ff3T79c0ff4Tff7b72]Tb4b4b4)
Tff7b72class T56d364MyViewModelTest Tb4b4b4: Te6edf3CommonMyViewModelTestTb4b4b4(Tb4b4b4) Tb4b4b4{
Tf0883e@BeforeTest
Tff7b72fun Td2a8ffsetupTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3setupTestContextTb4b4b4(Tb4b4b4) T8b949e// From :core:testing, initializes Robolectric context
Te6edf3setupRepoTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Ta5d6ff```

Tc9d1d9## Key Components

Tff7b72- **Test Doubles / Fakes**: Provides in-memory implementations of core repositories (e.g., Ta5d6ff`FakeNodeRepository`, Ta5d6ff`FakeMeshLogRepository`) to isolate components under test.
Tff7b72- **Coroutines Testing**: Provides dispatchers and test rules that replace the main dispatcher with Ta5d6ff`TestDispatcher` to allow time-control and synchronous execution of coroutines in tests.
Tff7b72- **Mokkery Support**: Integrated with the Mokkery compiler plugin to provide robust and unified mocking capabilities in Ta5d6ff`commonTest`.

Tc9d1d9## Usage
Add this module to your Ta5d6ff`commonTest` source set dependencies in your KMP module's Ta5d6ff`build.gradle.kts`:

Ta5d6ff```Ta5d6ffkotlin
Te6edf3kotlin Tb4b4b4{
Te6edf3sourceSets Tb4b4b4{
Te6edf3commonTestTb4b4b4.Te6edf3dependencies Tb4b4b4{
Te6edf3implementationTb4b4b4(Te6edf3projectsTb4b4b4.Te6edf3coreTb4b4b4.Te6edf3testingTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Ta5d6ff```

Served by rngit 1.5.0 - Generated in 0.09s